home *** CD-ROM | disk | FTP | other *** search
Modula Implementation | 1997-04-23 | 5.9 KB | 208 lines | [TEXT/3PRM] |
- implementation module font;
-
- import StdClass,StdInt, StdString, StdChar, StdBool,StdArray;
- import pointer, quickdraw, fonts;
- import commonDef;
-
- :: Font = { fontNum :: !FontNum,
- fontName :: !FontName,
- fontStyles :: ![FontStyle],
- fontSize :: !FontSize };
- :: FontNum :== Int;
- :: FontName :== String;
- :: FontStyle :== String;
- :: FontSize :== Int;
- :: FontInfo :== (!Int, !Int, !Int, !Int);
-
-
- MinFontSize :== 6;
- MaxFontSize :== 128;
-
- GrafPtrtxFont :== 68;
- GrafPtrtxFace :== 70;
- GrafPtrtxSize :== 74;
-
-
- FontAtts :: !Font -> (!FontNum, !FontName, ![FontStyle], !FontSize);
- FontAtts {fontNum=num, fontName=name, fontStyles=style, fontSize=size} = (num,name,style,size);
-
- SelectFont :: !FontName ![FontStyle] !FontSize -> (!Bool, !Font);
- SelectFont name=:"Chicago" style size
- = (exists, {fontNum=0, fontName=name, fontStyles=style, fontSize=size1});
- where {
- (exists,tb) = RealFont 0 size1 NewToolbox;
- size1 = SetBetween size MinFontSize MaxFontSize;
- };
- SelectFont name style size
- | fontNr == 0
- = (dExists, {fontNum=dFontNr, fontName=dName, fontStyles=style, fontSize=size1});
- = (exists, {fontNum= fontNr, fontName= name, fontStyles=style, fontSize=size1});
- where {
- (fontNr, tb ) = GetFNum name NewToolbox;
- (dFontNr, tb1) = GetFNum dName tb;
- (dExists, _) = RealFont dFontNr size1 tb1;
- (exists, _) = RealFont fontNr size1 tb;
- size1 = SetBetween size MinFontSize MaxFontSize;
- (dName, dStyle, dSize) = DefaultFont;
- };
-
- DefaultFont :: (!FontName, ![FontStyle], !FontSize);
- DefaultFont = ("Chicago", [], 12);
-
- FontNames :: [FontName];
- FontNames = FontNames` NewToolbox [0 : FromTo 2 255];
-
- FontNames` :: Toolbox [Int] -> [FontName];
- FontNames` tb [nr : nrs]
- | "" <> name = [name : names];
- = names;
- where {
- (name, tb1) = GetFontName nr String256 tb;
- names = FontNames` tb1 nrs;
- };
- FontNames` _ _ = [];
-
- FontStyles :: !FontName -> [FontStyle];
- FontStyles name
- = [ "Bold",
- "Italic",
- "Underline",
- "Outline",
- "Shadow",
- "Condense",
- "Extend"
- ];
-
- FontSizes :: !FontName -> [FontSize];
- FontSizes name
- | fontNr == 0 = FontSizes` tb1 dFontNr MinFontSize MaxFontSize;
- = FontSizes` tb fontNr MinFontSize MaxFontSize;
- where {
- (fontNr, tb ) = GetFNum name NewToolbox;
- (dFontNr, tb1) = GetFNum dName tb;
- (dName, dStyle, dSize) = DefaultFont;
- };
-
- FontSizes` :: !Toolbox !Int !Int !Int -> [FontSize];
- FontSizes` tb fontNr l u
- | l > u = [];
- | exists = [l : sizes];
- = sizes;
- where {
- (exists, tb1) = RealFont fontNr l tb;
- sizes = FontSizes` tb1 fontNr (inc l) u;
- };
-
-
- AccessFont :: !(!Toolbox -> !(!x, !Toolbox)) !Font -> x;
- AccessFont f {fontNum=nr,fontStyles=style,fontSize=size}
- = let! {
- tb4;
- } in x1;
- where {
- (gPtr, tb1) = QGetPort NewToolbox;
- (cFont, tb2) = GrafPtrGetFont gPtr tb1;
- (_, tb3) = GrafPtrSetFont (nr,StyleToStyleID style,size) (0,tb2);
- (x1, tb4) = GrafPtrSetFont cFont (f tb3);
- };
-
- FontCharWidth :: !Char !Font -> Int;
- FontCharWidth char font = AccessFont (QCharWidth char) font;
-
- FontCharWidths :: ![Char] !Font -> [Int];
- FontCharWidths chars font = AccessFont (GetCharWidths chars) font;
-
- GetCharWidths :: ![Char] !Toolbox -> (![Int], !Toolbox);
- GetCharWidths [c : cs] tb
- = ([cWidth : cWidths], tb2);
- where {
- (cWidth, tb1) = QCharWidth c tb;
- (cWidths,tb2) = GetCharWidths cs tb1;
- };
- GetCharWidths _ tb = ([], tb);
-
- FontStringWidth :: !{#Char} !Font -> Int;
- FontStringWidth string font = AccessFont (QStringWidth string) font;
-
- FontStringWidths :: ![{#Char}] !Font -> [Int];
- FontStringWidths strings font = AccessFont (GetStringWidths strings) font;
-
- GetStringWidths :: ![String] !Toolbox -> (![Int], !Toolbox);
- GetStringWidths [t : ts] tb
- = ([sWidth : sWidths], tb2);
- where {
- (sWidth, tb1) = QStringWidth t tb;
- (sWidths,tb2) = GetStringWidths ts tb1;
- };
- GetStringWidths _ tb = ([], tb);
-
- FontMetrics :: !Font -> FontInfo;
- FontMetrics font = AccessFont GetFontInfo font;
-
- GetFontInfo :: !Toolbox -> (!FontInfo, !Toolbox);
- GetFontInfo tb
- = ((ascent, descent, maxWidth, leading), tb1);
- where {
- (ascent, descent, maxWidth, leading, tb1) = QGetFontInfo tb;
- };
-
- GrafPtrSetFont :: !(!Int, !Int, !Int) !(!x, !Toolbox) -> (!x, !Toolbox);
- GrafPtrSetFont (nr, style, size) (x,tb)
- = (x, QTextSize size (QTextFace style (QTextFont nr tb)));
-
- GrafPtrGetFont :: !GrafPtr !Toolbox -> (!(!Int, !Int, !Int), !Toolbox);
- GrafPtrGetFont gPtr tb
- // = ((nr, style >> 8, size), tb3);
- = ((nr, style, size), tb3);
- where {
- (nr, tb1) = LoadWord (gPtr + GrafPtrtxFont) tb;
- (style, tb2) = LoadWord (gPtr + GrafPtrtxFace) tb1;
- (size, tb3) = LoadWord (gPtr + GrafPtrtxSize) tb2;
- };
-
-
- StyleToStyleID :: ![FontStyle] -> Int;
- StyleToStyleID ["Bold" : s] = Bold + StyleToStyleID s;
- StyleToStyleID ["Italic" : s] = Italic + StyleToStyleID s;
- StyleToStyleID ["Underline" : s] = Underline + StyleToStyleID s;
- StyleToStyleID ["Outline" : s] = Outline + StyleToStyleID s;
- StyleToStyleID ["Shadow" : s] = Shadow + StyleToStyleID s;
- StyleToStyleID ["Condense" : s] = Condense + StyleToStyleID s;
- StyleToStyleID ["Extend" : s] = Extend + StyleToStyleID s;
- StyleToStyleID [_ : s] = StyleToStyleID s;
- StyleToStyleID _ = 0;
-
-
- FontNameToLowCaps :: !FontName -> String;
- FontNameToLowCaps s = FontNameToLowCaps` s (dec (size s));
-
- FontNameToLowCaps` :: !FontName !Int -> String;
- FontNameToLowCaps` s (-1) = s;
- FontNameToLowCaps` s i
- | lowered
- = s` := (i, c`);
- = s`;
- where {
- s` = FontNameToLowCaps` s (dec i);
- (lowered,c`)= CharToLowChar c;
- c = s.[i];
- };
-
- CharToLowChar :: !Char -> (!Bool, !Char);
- CharToLowChar c
- | c >= 'A' && c <= 'Z' = (True, toChar (toInt c - toInt 'A' + toInt 'a'));
- = (False, c);
-
- FromTo :: !Int !Int -> [Int];
- FromTo m n
- | m < n = [m : FromTo (inc m) n];
- | m > n = [m : FromTo (dec m) n];
- = [m];
-
- String256 :: String;
- String256
- = string128 +++ string128;
- where {
- string128 = "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@";
- };
-